package com.yjk.mall.product.service.impl;

import com.yjk.mall.product.dao.AttrAttrgroupRelationDao;
import com.yjk.mall.product.dao.AttrDao;
import com.yjk.mall.product.entity.AttrAttrgroupRelationEntity;
import com.yjk.mall.product.entity.AttrEntity;

import com.yjk.mall.product.service.AttrService;
import com.yjk.mall.product.vo.AttrGroupResVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjk.common.utils.PageUtils;
import com.yjk.common.utils.Query;

import com.yjk.mall.product.dao.AttrGroupDao;
import com.yjk.mall.product.entity.AttrGroupEntity;
import com.yjk.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrGroupDao attrGroupDao;

    @Autowired
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(
                    i -> i.eq("attr_group_id", key).or().like("attr_group_name", key)
            );
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        } else {

            queryWrapper.eq("catelog_id", catelogId);

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrEntity> queryRelation(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> ids = entities.stream().map(entity -> entity.getAttrId()).collect(Collectors.toList());
        if (ids.size() != 0) {
            List<AttrEntity> attrEntities = (List<AttrEntity>) attrService.listByIds(ids);
            return attrEntities;
        }
        return null;
    }

    @Override
    public PageUtils queryNoRelation(Map<String, Object> params, Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
        Long catelogId=attrGroupEntity.getCatelogId();
        List<AttrGroupEntity> AttrGroupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<Long> gids = AttrGroupEntities.stream().map(entity -> entity.getAttrGroupId()).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> AttrAttrgroupRelationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", gids));
        List<Long> aids = AttrAttrgroupRelationEntities.stream().map(relEntity -> relEntity.getAttrId()).collect(Collectors.toList());
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).eq("attr_type", 1).notIn("attr_id", aids);
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupResVo> withAttr(Long catelogId) {
        List<AttrGroupEntity> entities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupResVo> vos = entities.stream().map(entity -> {
            AttrGroupResVo vo = new AttrGroupResVo();
            BeanUtils.copyProperties(entity, vo);
            Long attrGroupId = entity.getAttrGroupId();
            List<AttrAttrgroupRelationEntity> attrEntities =
                    relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
            List<Long> ids = attrEntities.stream().map(attrEntity -> attrEntity.getAttrId()).collect(Collectors.toList());
            List<AttrEntity> attrs = attrDao.selectList(new QueryWrapper<AttrEntity>().in("attr_id",ids));
            vo.setAttrs(attrs);
            return vo;
        }).collect(Collectors.toList());
        return vos;











    }
}